/**
 * Created by Administrator on 2016/9/23.
 */

function rCloud(token,nick_name,chatroom_id,user_id,chat_name) {
    $('#chat_room_id').append("<span style='color:#f69200;'>欢迎加入"+nick_name+"的直播间</span>");
    // 设置连接监听状态 （ status 标识当前连接状态）
    // 连接状态监听器
    RongIMClient.setConnectionStatusListener({
        onChanged: function (status) {
            switch (status) {
                //链接成功
                case RongIMLib.ConnectionStatus.CONNECTED:

                    $('#chat_room_id').append("<span class='span_block' style='color:#f69200;'>弹幕连接成功</span>");
                    break;
                //正在链接
                case RongIMLib.ConnectionStatus.CONNECTING:
                    $('#chat_room_id').append("<span class='span_block' style='color:#f69200;'>弹幕连接中...</span>");
                    break;
                //重新链接
                case RongIMLib.ConnectionStatus.DISCONNECTED:
                    $('#chat_room_id').append("<span class='span_block'  style='color:#f69200;'>弹幕已断开</span>");
                    break;
                //其他设备登录
                case RongIMLib.ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT:
                    console.log('其他设备登录');
                    break;
                //网络不可用
                case RongIMLib.ConnectionStatus.NETWORK_UNAVAILABLE:
                    $('#chat_room_id').append("<span class='span_block' style='color:#377ade;'>加入直播间出错了,正在尝试重连...</span>");
                    console.log('网络不可用');
                    break;
            }
        }
    });

    // 消息监听器
    RongIMClient.setOnReceiveMessageListener({
        // 接收到的消息
        onReceived: function (message) {
            // 判断消息类型
            switch (message.messageType) {
                case RongIMClient.MessageType.TextMessage:

                    // 发送的消息内容将会被打印
                    break;
//
                case  RongIMClient.MessageType.VoiceMessage:
                    // 对声音进行预加载
                    // message.content.content 格式为 AMR 格式的 base64 码
                    RongIMLib.RongIMVoice.preLoaded(message.content.content);
                    break;
                case RongIMClient.MessageType.ImageMessage:
                    // do something...
                    break;
                case  RongIMClient.MessageType.DiscussionNotificationMessage
                :
                    // do something...
                    break;
                case
                RongIMClient.MessageType.LocationMessage
                :
                    // do something...
                    break;
                case
                RongIMClient.MessageType.RichContentMessage
                :
                    // do something...
                    break;
                case
                RongIMClient.MessageType.DiscussionNotificationMessage
                :
                    // do something...
                    break;
                case
                RongIMClient.MessageType.InformationNotificationMessage
                :
                    // do something...
                    break;
                case
                RongIMClient.MessageType.ContactNotificationMessage
                :
                    // do something...
                    break;
                case
                RongIMClient.MessageType.ProfileNotificationMessage
                :
                    // do something...
                    break;
                case
                RongIMClient.MessageType.CommandNotificationMessage
                :
                    // do something...
                    break;
                case
                RongIMClient.MessageType.CommandMessage
                :
                    // do something...
                    break;
                case
                RongIMClient.MessageType.UnknownMessage
                :
                {
                    console.log(message);
                    if (typeof(message.content.user) == 'undefined') {

                        if (typeof(message.content.extra) != 'undefined') {

                            var joinRoom = message.content.extra;
                            var obj = $.parseJSON(joinRoom);

                            if (typeof(message.content.item_id) != 'undefined') {

                                var item_id = message.content.item_id;
                                if ((item_id == 200) || (item_id == 201) || (item_id == 202)) {

                                    if (typeof (obj.textColor) != 'undefined') {

                                        if (typeof (obj.action) != 'undefined') {
                                            $('#chat_room_id').append("<span class='span_block' style='color: #" + obj.textColor + "'>" + message.content.content.replace("%s", obj.action.name) + "</span>");
                                            sendDanmu(message.content.content.replace("%s", obj.action.name),"#f69200");
                                        } else {
                                            $('#chat_room_id').append("<span class='span_block' style='color: #" + obj.textColor + "'>" + message.content.content + "</span>");
                                            sendDanmu(message.content.content,"#f69200");
                                        }

                                    } else {
                                        $('#chat_room_id').append("<span class='span_block'>" + message.content.content.replace("%s", obj.action.name) + "</span>");
                                        sendDanmu(message.content.content.replace("%s", obj.action.name),"#f69200");
                                    }

                                } else {

                                    if (typeof (obj.textColor) != 'undefined') {
                                        $('#chat_room_id').append("<span class='span_block' style='color:#" + obj.textColor + "'>" + message.content.content + "</span>");
                                        sendDanmu(message.content.content,"#f69200");
                                    }

                                }

                            }

                        }
                        document.getElementById("chat_room_id").scrollTop += 25;
                    } else {

                        if (typeof(message.content.extra) == 'undefined') {
                            $('#chat_room_id').append("<span class='span_block'><span style='color:#2b77df;'>" + message.content.user.name + ":</span><span>" + message.content.content + "</span></span>");
                            sendDanmu(message.content.content,"#f69200");
                        } else {

                            var msgStr = message.content.extra;
                            var extraObj = $.parseJSON(msgStr);

                            if (typeof(message.content.item_id) != 'undefined') {

                                var item_id = message.content.item_id;
                                if ((item_id == 200) || (item_id == 201) || (item_id == 202)) {
                                    if (typeof(extraObj.textColor) != 'undefined') {
                                        $('#chat_room_id').append("<span class='span_block' style='color: #" + extraObj.textColor + "'>" + message.content.content + "</span>");
                                        sendDanmu(message.content.content,"#f69200");
                                    }

                                }else if(item_id == 1001){
                                    if (typeof (extraObj.action) != 'undefined') {
                                        var actionobj=$.parseJSON(extraObj.action);
                                        $('#chat_room_id').append("<span class='span_block' style='color: #f69200''>" + message.content.user.name +"送给主播<img style='width:20px;height:20px;'src='../../../shihousharenew/static/js/"+actionobj.gift.pic+"'>"+ "</span>");
                                        sendDanmu(message.content.content.replace("%s", extraObj.action.name),"#f69200");
                                    } else {
                                    }

                                } else {
                                    if (typeof(extraObj.textColor) != 'undefined') {

                                        if (typeof (extraObj.superId) != 'undefined') {

                                            if (typeof(extraObj.nickColor) != 'undefined') {
                                                $('#chat_room_id').append("<span class='span_block' ><span style='color:#" + extraObj.nickColor + "; '>" + message.content.user.name + ":</span><span style='color: #" + extraObj.textColor + "'>" + message.content.content + "</span></span>");
                                                sendDanmu(message.content.content,"#f69200");
                                            } else {
                                                $('#chat_room_id').append("<span class='span_block'><span >" + message.content.user.name + ":</span><span style='color: #" + extraObj.textColor + "'>" + message.content.content + "</span></span>");
                                                sendDanmu(message.content.content,"#f69200");
                                            }

                                        } else {
                                            $('#chat_room_id').append("<span class='span_block'><span style='color:#2b77df;'>" + message.content.user.name + ":</span><span style='color: #" + extraObj.textColor + "'>" + message.content.content + "</span></span>");
                                            sendDanmu(message.content.content,"#f69200");
                                        }

                                    } else {
                                        $('#chat_room_id').append("<span class='span_block'><span style='color:#2b77df;'>" + message.content.user.name + ":</span> <span>" + message.content.content + "</span></span>");
                                        sendDanmu(message.content.content,"#f69200");
                                    }
                                    document.getElementById("chat_room_id").scrollTop += 25;
                                }
                            }

                        }

                        document.getElementById("chat_room_id").scrollTop += 25;

                    }
                    break;
                }
                default:
                // 自定义消息
                // do something...
            }
        }
    });

//定义过来的消息为UnknownMessage
    var messageName = "UnknownMessage"; // 消息名称。
    var objectName = "RC:RichMsg"; // 消息内置名称，请按照此格式命名。
    var mesasgeTag = new RongIMLib.MessageTag(true, true);// 消息是否保存是否计数，true true 保存且计数，false false 不保存不计数。
    var propertys = ["content", "extra", "item_id", "user"]; // 消息类中的属性名。
    RongIMClient.registerMessageType(messageName, objectName, mesasgeTag, propertys);

    // 连接融云服务器。
    RongIMClient.connect(token, {
        onSuccess: function (userId) {
            var chatRoomId = chatroom_id; // 聊天室 Id。
//                var chatRoomId = "10075_2387"; // 聊天室 Id。
            var count = 10;// 拉取最近聊天最多 50 条。
            RongIMClient.getInstance().joinChatRoom(chatRoomId, count, {
                onSuccess: function () {
                    // 加入聊天室成功。
                    $('#chat_room_id').append("<span class='span_block' style='color:#f69200;'>聊天室连接成功</span>");
                },
                onError: function (error) {
                    // 加入聊天室失败
                    $('#chat_room_id').append("<span class='span_block' style='color:#f69200;'>聊天室连接失败</span>");
                }
            });
        },
        onTokenIncorrect: function () {
            console.log('token无效');
        },
        onError: function (errorCode) {
            var info = '';
            switch (errorCode) {
                case RongIMLib.ErrorCode.TIMEOUT:
                    info = '超时';
                    break;
                case RongIMLib.ErrorCode.UNKNOWN_ERROR:
                    info = '未知错误';
                    break;
                case RongIMLib.ErrorCode.UNACCEPTABLE_PaROTOCOL_VERSION:
                    info = '不可接受的协议版本';
                    break;
                case RongIMLib.ErrorCode.IDENTIFIER_REJECTED:
                    info = 'appkey不正确';
                    break;
                case RongIMLib.ErrorCode.SERVER_UNAVAILABLE:
                    info = '服务器不可用';
                    break;
            }
            console.log("聊天室错误：" + errorCode);
        }
    });
    <!-- rongyun chat end -->
    <!-- rongyun sendMsg start -->
    $('#sendMsg').click(function () {
        var send_msg_data = $("#danMsg").val();
        if (send_msg_data == '' || send_msg_data == 'undefined') {
            alert('发送消息不能为空');
            return false;
        } else {
            // 定义消息类型,文字消息使用 RongIMLib.TextMessage
            var extraMsg = '{"textColor":"000000", "action":"[]"}';
            var msg = new RongIMClient.RegisterMessage.UnknownMessage({
                content: $("#danMsg").val(),
                extra: extraMsg,
                item_id: "100",
                user: {id: user_id, name: chat_name }
            });
            //或者使用RongIMLib.TextMessage.obtain 方法.具体使用请参见文档
            //var msg = RongIMLib.TextMessage.obtain("hello");
            var conversationtype = RongIMLib.ConversationType.CHATROOM; // 私聊
            var targetId = chatroom_id; // 目标 Id
//                var targetId = "10075_2387"; // 目标 Id
            RongIMClient.getInstance().sendMessage(conversationtype, targetId, msg, {
                    // 发送消息成功
                    onSuccess: function (message) {
                        //message 为发送的消息对象并且包含服务器返回的消息唯一Id和发送消息时间戳
                        console.log("Send successfully");
                        console.log(message);
                        $('#chat_room_id').append("<span class='span_block'><span style='color:#f69200" + ";'>"+chat_name+":</span><span style='color:#999" + "'>" + send_msg_data + "</span></span>");
                        sendDanmu(send_msg_data,"#f69200");
                        document.getElementById("chat_room_id").scrollTop += 25;
                    },
                    onError: function (errorCode, message) {
                        var info = '';
                        switch (errorCode) {
                            case RongIMLib.ErrorCode.TIMEOUT:
                                info = '超时';
                                break;
                            case RongIMLib.ErrorCode.UNKNOWN_ERROR:
                                info = '未知错误';
                                break;
                            case RongIMLib.ErrorCode.REJECTED_BY_BLACKLIST:
                                info = '在黑名单中，无法向对方发送消息';
                                break;
                            case RongIMLib.ErrorCode.NOT_IN_DISCUSSION:
                                info = '不在讨论组中';
                                break;
                            case RongIMLib.ErrorCode.NOT_IN_GROUP:
                                info = '不在群组中';
                                break;
                            case RongIMLib.ErrorCode.NOT_IN_CHATROOM:
                                info = '不在聊天室中';
                                break;
                            default :
                                info = "其他错误";
                                break;
                        }
                        console.log('发送失败:' + info);
                    }
                }
            );
            $("#danMsg").val("");
        }

    });
}

<!-- rongyun sendMsg end -->
//        }); <!-- get接口 -->
